import http from '../../../class/Utils/Http.js'
import tips from '../../../class/Utils/Tips.js'
import user from '../../../class/Utils/User.js'
var WxParse = require('../../../class/wxParse/wxParse.js');
Page({
  data: {
    mobanid: '',
    flag:0,
    info:[],
    param:{},
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 200,
  },
  onLoad: function (p) {
    var that   = this
    var param  = that.data.param
    param.cid  = p.cid || 85606
    param.time = p.time || 152158
    param.id   = p.id || 3629384
    that.setData({
      param: param
    })
    var menus = p.menus
    if (menus) {
      that.setData({
        menus: menus
      })
    }
    var mid = p.mid
    if (mid) {
      that.setData({
        mid: mid
      })
    }
    var userid = p.userid || '0'
    that.setData({
      userid: userid
    })
    var mobanid = p.mobanid || 1
    that.setData({
      mobanid: mobanid
    })
    wx.getSystemInfo({
      success: function (res) {
        var pageWidth = res.windowWidth;
        that.setData({
          nbannerHeight: pageWidth
        });
      }
    });
    console.log(that.data.nbannerHeight)
    

    that.getdata()
  },
  getdata:function(){
    var that = this
    http.reqload("/company/product", that.data.param, function (res) {
      console.log(res)
      
      // 判断是否使用模板 begin
      // if (res.Company.company_style) {
      //   if (res.Company.company_style.wx_style) {
      //     that.setData({
      //       mobanid: res.Company.company_style.wx_style,
      //     })
      //   }
      // }else {
      //   that.setData({
      //     mobanid: 1,
      //   })
      // }
      // 判断是否使用模板 end
      console.log('模板号 ' + that.data.mobanid)



      that.setData({
        info: res
      })
      WxParse.wxParse('content', 'html', res.product.intro, that, 5);
      var user = wx.getStorageSync('user')
      if(user.log==1){
        that.getfollow()
      }
    })
    
  },
  // 获取关注状态
  getfollow:function(cd){
    var that = this
    var data = {
      uid:wx.getStorageSync('user').userid,
      type:2,
      fid:that.data.info.product.id
    }
    http.reqload("/api/is_shoucang", data, function (res) {
      console.log(res)
      if(res.code == '1304') {
        that.setData({
          flag:1,
        })
      }
    })
  },
  // 关注商品
  gofollow:function(){
    var that = this
    var data = {
      uid:wx.getStorageSync('user').userid,
      type:2,
      fid:that.data.info.product.id,
      fname:that.data.info.product.name
    }
    http.reqload("/api/shoucang", data, function (res) {
      console.log(res);
      if(res.code == '200') {
        that.setData({
          flag:1,
        })
        tips.ok(res.msg_val)
      }else{
        tips.error(res.msg_val)
      }
    })
  },
  focus:function(e){
    var   that=this;
    user.log_user(this, function (res) {
       var focusflag=that.data.flag;
       if(focusflag==0){
        that.gofollow()
       }else{
        tips.ok("你已经关注过了");
       }
    })
  },
  //去首页
  gotohome: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },

  /*
     授权登录
     */
    bindGetUserInfo: function (e) {
        var that = this
        user.user(this, function (res) {
            that.setData({
                shouquan: 0,
                userinfo: res

            })
            if (res.log == 0) {
                that.setData({
                    bangdingtoocletag: true
                })
            }
        })

    },
    /*
     弹出 登录
     */
    bangdingtoocle: function () {
        var that = this
        that.setData({
            bangdingtoocletag: !that.data.bangdingtoocletag
        })
    },
    /*
     点击登录
     */
    denglu: function (e) {
        var that = this
        var zhanghao = e.detail.value.zhanghao
        var mima = e.detail.value.mima
        if (!zhanghao) {
            tips.error("账号不能为空");
            return false
        }
        if (!mima) {
            tips.error("密码不能为空");
        }
        http.reqload("/Api/log", {login: zhanghao, passwd: mima, uid: wx.getStorageSync('user').userid}, function (res) {
            console.log(res)
            if (res.code == '200') {
                var user = wx.getStorageSync('user')
                user.log = 1
                wx.setStorageSync('user', user)
                tips.ok('登录成功')
                that.setData({
                    bangdingtoocletag: false,
                    shengyihao: zhanghao
                })
            } else {
                tips.error(res.msg_val)
            }
        })
    },
  //获取绑定微信的手机号码
  getPhoneNumber: function (e) {
    var that = this
    user.getPhoneNumber(e, that)
  },
  //去名片
  gotowodemingpian: function (e) {
    var that = this
    wx.redirectTo({
      url: '/pages/mingpian/card/card?id=' + that.data.mid
    })
  },
})
